<template>
    <div class="recommend-list" v-loading="loading">
        <div class="flex-input-box">
            <div class="input-box">
                <!-- <span class="input-title" style="margin-left:0px;">关键字搜索</span> -->
                <el-input
                    placeholder="搜索推荐标题关键字"
                    @keyup.enter.native="selectChange"
                    style="width:200px;"
                    clearable
                    v-model.trim="keyWord">
                    <i slot="prefix" class="el-input__icon el-icon-search"></i>
                </el-input>

                <!-- <span class="input-title">状态筛选</span> -->
                <el-select
                    @change="selectChange"
                    style="width:100px;margin-left:10px"
                    v-model="status">
                    <el-option label="全部" :value="null"></el-option>
                    <el-option label="展示中" :value="1"></el-option>
                    <el-option label="已暂停" :value="0"></el-option>
                </el-select>

                <el-select
                    @change="selectChange"
                    style="width:120px;margin-left:10px"
                    v-model="categoryId">
                    <el-option v-for="(item, index) in typeList" :key="index" :label="item.name" :value="item.id"></el-option>
                </el-select>
                <el-button type="primary" class="btn" style="margin-left:10px" @click="getList(1)">搜索</el-button>
                <el-button type="success" class="btn" @click="goRecommendDetail(0)">新建</el-button>
            </div>

            <div class="right-btn">
                <el-button type="primary" class="btn" @click="showRecommendSort = true">推荐排序</el-button>
                <el-button class="btn" style="margin-left:10px" @click="showTypeDialog = true">编辑类别</el-button>
            </div>
        </div>

        <div class="table-box">
            <el-table
                :data="tableData"
                border
                style="width: 100%">
                <el-table-column prop="id" label="ID" width="60"></el-table-column>

                <el-table-column prop="title" label="推荐标题"></el-table-column>

                <el-table-column prop="categoryName" label="类别"></el-table-column>

                <el-table-column prop="status" label="状态">
                    <template slot-scope="scope">
                        <div style="color: #67C23A;" v-if="scope.row.status === 1">
                            <span class="point"></span>
                            <span>展示中</span>
                        </div>
                        <div style="color:#F56C6C;" v-if="scope.row.status === 0">
                            <div class="point red-point"></div>
                            <span>已暂停</span>
                        </div>
                    </template>
                </el-table-column>

                <el-table-column label="修改时间" width="110">
                    <template slot-scope="scope">
                        <span>{{scope.row.updateTime | timeInit}}</span>
                    </template>
                </el-table-column>

                <el-table-column label="创建时间" width="110">
                    <template slot-scope="scope">
                        <span>{{ scope.row.createTime | timeInit }}</span>
                    </template>
                </el-table-column>

                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <div v-if="scope.row.auditstatus !== -1">
                            <el-button v-if="scope.row.status === 1" @click="changeStatus(scope.row, 0)" type="text" size="small" style="color:red;">暂停</el-button>
                            <el-button v-if="scope.row.status === 0" @click="changeStatus(scope.row, 1)" type="text" size="small" style="color:#67C23A;">展示</el-button>
                        </div>
                        <!-- <div>
                            <el-button @click="delDetail(scope.row)" type="text" size="small" style="color:red;">删除</el-button>
                        </div> -->
                        <div>
                            <el-button @click="goRecommendDetail(scope.row.id)" type="text" size="small">查看详情</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <typeSortDialog v-model="showTypeDialog"/>

        <recommendSortDialog v-model="showRecommendSort"/>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'
import typeSortDialog from './typeSortDialog'
import recommendSortDialog from './recommendSortDialog'
export default {
    data () {
        return {
            showTypeDialog: false, // 类型弹窗
            showRecommendSort: false, // 推荐排序
            keyWord: null,
            status: null,
            categoryId: null, // 类型id
            typeList: [
                { name: '全部', id: null }
            ],
            tableData: [],
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            loading: false
        }
    },

    created () {
        this.getTypeList()
    },

    activated () {
        this.getList(this.pageNum)
    },

    methods: {
        // 获取类型列表
        async getTypeList () {
            let params = {
                pageNum: 1,
                pageSize: 100
            }
            let res = await this.$http.get('/trade/selectActivityCategory', {
                params
            })
            if (res.code === '0' && res.data && res.data.list) {
                this.typeList.push(...res.data.list)
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
        },

        // 获取列表
        async getList (pageNum) {
            if (this.loading) return
            this.pageNum = pageNum
            this.loading = true
            this.listParams = {
                status: this.status,
                categoryId: this.categoryId,
                keyWord: this.keyWord,
                pageNum: pageNum,
                pageSize: this.pageSize
            }

            let res = await this.$http.get('/trade/selectActivityLoad', {
                params: this.listParams
            })
            if (res.code === '0') {
                this.tableData = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },

        selectChange () {
            this.getList(1)
        },

        // 页码切换
        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getList(1)
        },

        // 页容切换
        pageChange (val) {
            this.pageNum = val
            this.getList(this.pageNum)
        },

        // 切换状态
        changeStatus (item, status) {
            let params = {
                id: item.id,
                status
            }
            this.$http.post('/trade/createActivityLoad', params).then(res => {
                if (res.code === '0') {
                    let message = status === 0 ? '已暂停' : '已展示'
                    item.status = status
                    this.$message({
                        type: 'success',
                        message
                    })
                } else {
                    this.$message({
                        type: 'error',
                        message: res.desc
                    })
                }
            })
        },

        // 删除
        delDetail (item, index) {
            this.$confirm(`此操作将永久删除该推荐 ${item.title} , 是否继续?`, '温馨提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                })
                this.tableData.splice(index, 1)
            }).catch(() => {
            })
        },

        // 查看推荐详情
        goRecommendDetail (id) {
            let query = {}
            if (id) {
                query.id = id
            }
            this.$router.push({
                path: '/wzry/recommendDetail',
                query
            })
        }
    },

    components: {
        typeSortDialog,
        recommendSortDialog
    },

    filters: {
        timeInit (val) {
            if (val) {
                return formatDate(val, 'YYYY-MM-DD HH:MM:SS')
            } else {
                return '-'
            }
        }
    }
}
</script>

<style lang="scss" scoped>
    .recommend-list {
        .flex-input-box {
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .input-box {
            font-size: 13px;
            font-weight: 500;
            .input-title {
                margin: 0 10px 0 20px;
            }
            .btn {
                margin-left: 10px;
            }
        }

        .table-box {
            margin: 20px 0;
            .cover {
                width: 80px;
                height: 80px;
                > img {
                    width: 100%;
                    height: 100%;
                }
            }
            .desc {
                overflow: hidden;
                line-height: 20px;
                -webkit-line-clamp: 5;
                -webkit-box-orient: vertical;
                display: -webkit-box;
                height: 100px;
            }
            .user-icon {
                width: 20px;
                height: 20px;
                border-radius: 50%;
                margin-right: 5px;
                vertical-align: top;
            }
            .table-btn {
                cursor: pointer;
                color: #409EFF;
                font-size: 13px;
                font-weight: 500;
                &:hover {
                    text-decoration: underline;
                }
            }
            .point {
                display:inline-block;
                width:10px;
                height:10px;
                border-radius: 50%;
                background-color:#67C23A;
                margin-right:3px;
            }
            .red-point {
                background-color: #F56C6C;
            }
        }

        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
